package cn.harry.controller.wx;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.CommonPage;
import cn.harry.common.api.CommonResult;
import cn.harry.common.enums.BusinessType;
import cn.harry.wx.entity.WxMsg;
import cn.harry.wx.form.WxMsgReplyForm;
import cn.harry.wx.service.MsgReplyService;
import cn.harry.wx.service.WxMsgService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 微信消息
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Api(tags = "wx-wxmsg => 微信消息")
@RestController
@RequestMapping("wx/wxmsg")
public class WxMsgController {
    @Resource
    private WxMsgService wxMsgService;
    @Autowired
    private MsgReplyService msgReplyService;


    @ApiOperation("list => 分页获取微信消息列表")
    @PreAuthorize("@ss.hasPermi('wx:wxmsg:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<WxMsg>> list(WxMsg wxMsg,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<WxMsg> page = wxMsgService.getPage(wxMsg, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 查询微信消息详细
     */
    @ApiOperation("{id} => 获取微信消息信息")
    @PreAuthorize("@ss.hasPermi('wx:wxmsg:query')")
    @GetMapping(value = "/{id}")
    public CommonResult<WxMsg> getInfo(@PathVariable Long id) {
        return CommonResult.success(wxMsgService.selectById(id));
    }

    @ApiOperation("create => 新建微信消息")
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody WxMsg wxMsg) {
        int count = wxMsgService.create(wxMsg);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("update/{id} => 修改指定微信消息")
    @PreAuthorize("@ss.hasPermi('wx:wxmsg:edit')")
    @SysLog(title = "微信消息数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id, @RequestBody WxMsg wxMsg) {
        int count = wxMsgService.update(id, wxMsg);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("deleteByIds/{ids} => 删除指定微信消息")
    @PreAuthorize("@ss.hasPermi('wx:wxmsg:remove')")
    @SysLog(title = "微信消息类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<Integer> deleteByIds(@PathVariable Long[] ids) {
        int count = wxMsgService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 回复
     */
    @PostMapping("/reply")
    @PreAuthorize("@ss.hasPermi('wx:wxmsg:add')")
    @ApiOperation(value = "回复")
    public CommonResult reply(@CookieValue String appid, @RequestBody WxMsgReplyForm form) {

        msgReplyService.reply(form.getOpenid(), form.getReplyType(), form.getReplyContent());
        return CommonResult.success();
    }
}
